package com.timothy.statistics;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.util.XMLUtil;
import com.timothy.android.view.BaseBean;
import com.timothy.android.view.PageBaseBean;
import com.timothy.answer.ChoiceResult;

public class AnswerBuilder {

	public final static Integer CHOICE_COUNT_PER_PAGE = 8;
	
	/**
	 * 
	 * @param content
	 * @return
	 */
	public static BaseBean parseBaseBean(String content){
		BaseBean baseBean = new BaseBean();
		
		//set question type
		String type = XMLUtil.getPropertyValue( content, "Type");
		baseBean.setQuestionType(Integer.valueOf(type));
		
		//set questionIndex
		String slideIndex = XMLUtil.getPropertyValue( content , "SlideIndex");
		baseBean.setQuestionIndex(Integer.valueOf(slideIndex));

		//set totalQuestionCount
		String totalQuestionCount = XMLUtil.getPropertyValue( content , "TotalQuestionCount");
		if(totalQuestionCount!=null&&!"".equals(totalQuestionCount.trim())){
			baseBean.setTotalQuestionCount(Integer.valueOf(totalQuestionCount));
		}else{
			baseBean.setTotalQuestionCount(6);
		}
		
		//set questionName
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		baseBean.setQuestionText(questionName);
		
		//set answerTextStyle
		String answerTextStyle = XMLUtil.getPropertyValue( content , "AnswerTextStyle");
		baseBean.setAnswerTextStyle(Integer.valueOf(answerTextStyle));
		//set TotalPages
		List<String> choiceStrArray = XMLUtil.getChildren( content );
		
		//set charts per page
		baseBean.setNumPerPage(CHOICE_COUNT_PER_PAGE);
		
		int totalCount=0;
		int totalChosen=0;
		int page = 1;
		
		PageBaseBean pageBean = null;//initial page bean
//		List<ChoiceResult> pageList = new ArrayList<ChoiceResult>();//initial page list

		for( String choiceStr: choiceStrArray ) {
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
			totalChosen+= Integer.valueOf(chosen);
		}
		
		for( String choiceStr: choiceStrArray ) {
			if(totalCount==0){
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
			}else if(totalCount%CHOICE_COUNT_PER_PAGE==0){
//				pageBean.setCrList(pageList);
				baseBean.addPageBaseBean(pageBean);
				
				//initial page bean
//				pageList = new ArrayList<ChoiceResult>();
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
				
			}
			
			String index = XMLUtil.getPropertyValue( choiceStr , "Index");
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
			String text = XMLUtil.getPropertyValue( choiceStr , "Text");
			
			ChoiceResult cr = new ChoiceResult(Integer.valueOf(index),Integer.valueOf(chosen),text,text);
			cr.setShare((Double.valueOf(chosen))/totalChosen);
			pageBean.addChoiceResult(cr);
			
			totalCount++;
		}
		
//		pageBean.setCrList(pageList);
		baseBean.setTotalPages(page-1);
		baseBean.setTotalCount(totalCount);
		baseBean.addPageBaseBean(pageBean);
		
		return baseBean;
	}
	
	public static BaseBean parseBaseBeanForGrading(String content){
		BaseBean baseBean = new BaseBean();
		
		//set question type
		String type = XMLUtil.getPropertyValue( content, "Type");
		baseBean.setQuestionType(Integer.valueOf(type));
		
		//set questionIndex
		String slideIndex = XMLUtil.getPropertyValue( content , "SlideIndex");
		baseBean.setQuestionIndex(Integer.valueOf(slideIndex));

		//set totalQuestionCount
		String totalQuestionCount = XMLUtil.getPropertyValue( content , "TotalQuestionCount");
		if(totalQuestionCount!=null&&!"".equals(totalQuestionCount.trim())){
			baseBean.setTotalQuestionCount(Integer.valueOf(totalQuestionCount));
		}else{
			baseBean.setTotalQuestionCount(6);
		}
		
		//set questionName
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		baseBean.setQuestionText(questionName);
		
		//set answerTextStyle
		String answerTextStyle = XMLUtil.getPropertyValue( content , "AnswerTextStyle");
		baseBean.setAnswerTextStyle(Integer.valueOf(answerTextStyle));
		//set TotalPages
		List<String> choiceStrArray = XMLUtil.getChildren( content );
		
		//set charts per page
		baseBean.setNumPerPage(CHOICE_COUNT_PER_PAGE);
		
		int totalCount=0;
		int totalChosen=0;
		int page = 1;
		
		PageBaseBean pageBean = null;//initial page bean
//		List<ChoiceResult> pageList = new ArrayList<ChoiceResult>();//initial page list

		for( String choiceStr: choiceStrArray ) {
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Total");
			totalChosen+= Integer.valueOf(chosen);
		}
		
		for( String choiceStr: choiceStrArray ) {
			if(totalCount==0){
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
			}else if(totalCount%CHOICE_COUNT_PER_PAGE==0){
//				pageBean.setCrList(pageList);
				baseBean.addPageBaseBean(pageBean);
				
				//initial page bean
//				pageList = new ArrayList<ChoiceResult>();
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
				
			}
			
			String index = XMLUtil.getPropertyValue( choiceStr , "Index");
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Total");
			String text = XMLUtil.getPropertyValue( choiceStr , "Text");
			
			ChoiceResult cr = new ChoiceResult(Integer.valueOf(index),Integer.valueOf(chosen),text,text);
			cr.setShare((Double.valueOf(chosen))/totalChosen);
			pageBean.addChoiceResult(cr);
			
			totalCount++;
		}
		
//		pageBean.setCrList(pageList);
		baseBean.setTotalPages(page-1);
		baseBean.setTotalCount(totalCount);
		baseBean.addPageBaseBean(pageBean);
		
		return baseBean;
	}

	public static BaseBean parseBaseBeanForFreeForm(String content){
		BaseBean baseBean = new BaseBean();
		
		//set question type
		String type = XMLUtil.getPropertyValue( content, "Type");
		baseBean.setQuestionType(Integer.valueOf(type));
		
		//set questionIndex
		String slideIndex = XMLUtil.getPropertyValue( content , "SlideIndex");
		baseBean.setQuestionIndex(Integer.valueOf(slideIndex));

		//set totalQuestionCount
		String totalQuestionCount = XMLUtil.getPropertyValue( content , "TotalQuestionCount");
		if(totalQuestionCount!=null&&!"".equals(totalQuestionCount.trim())){
			baseBean.setTotalQuestionCount(Integer.valueOf(totalQuestionCount));
		}else{
			baseBean.setTotalQuestionCount(6);
		}
		
		//set questionName
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		baseBean.setQuestionText(questionName);
		
		//set answerTextStyle
		String answerTextStyle = XMLUtil.getPropertyValue( content , "AnswerTextStyle");
		baseBean.setAnswerTextStyle(Integer.valueOf(answerTextStyle));
		//set TotalPages
		List<String> choiceStrArray = XMLUtil.getChildren( content );
		
		//set charts per page
		baseBean.setNumPerPage(CHOICE_COUNT_PER_PAGE);
		
		int totalCount=0;
		int totalChosen=0;
		int page = 1;
		
		PageBaseBean pageBean = null;//initial page bean

		for( String choiceStr: choiceStrArray ) {
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
			totalChosen+= Integer.valueOf(chosen);
		}
		
		for( String choiceStr: choiceStrArray ) {
			if(totalCount==0){
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
			}else if(totalCount%CHOICE_COUNT_PER_PAGE==0){
				baseBean.addPageBaseBean(pageBean);
				
				//initial page bean
				pageBean = new PageBaseBean();
				pageBean.setPage(page++);
				
			}
			
			String index = XMLUtil.getPropertyValue( choiceStr , "Index");
			String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
			String text = XMLUtil.getPropertyValue( choiceStr , "Mark");
			
			ChoiceResult cr = new ChoiceResult(Integer.valueOf(index),Integer.valueOf(chosen),text,text);
			cr.setShare((Double.valueOf(chosen))/totalChosen);
			pageBean.addChoiceResult(cr);
			
			totalCount++;
		}
		
		baseBean.setTotalPages(page-1);
		baseBean.setTotalCount(totalCount);
		baseBean.addPageBaseBean(pageBean);
		
		return baseBean;
	}
	
	public static BaseBean parseBaseBeanForPriority(String content,String chooseLevel){
		BaseBean baseBean = new BaseBean();
		
		List<String> priorityList = XMLUtil.getChildren( content );
		
		//set question type
		String type = XMLUtil.getPropertyValue( content, "Type");
		baseBean.setQuestionType(Integer.valueOf(type));
		
		//set questionIndex
		String slideIndex = XMLUtil.getPropertyValue( content , "SlideIndex");
		baseBean.setQuestionIndex(Integer.valueOf(slideIndex));

		//set totalQuestionCount
		String totalQuestionCount = XMLUtil.getPropertyValue( content , "TotalQuestionCount");
		if(totalQuestionCount!=null&&!"".equals(totalQuestionCount.trim())){
			baseBean.setTotalQuestionCount(Integer.valueOf(totalQuestionCount));
		}else{
			baseBean.setTotalQuestionCount(6);
		}
		
		//set questionName
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		//set answerTextStyle
		String answerTextStyle = XMLUtil.getPropertyValue( content , "AnswerTextStyle");
		
		
		baseBean.setQuestionText(questionName);
		baseBean.setAnswerTextStyle(Integer.valueOf(answerTextStyle));
		
		int sum = 0;
		for( String priority: priorityList ) {
			String level = XMLUtil.getPropertyValue( priority , "level");
			if(!level.equalsIgnoreCase(chooseLevel)){
				continue;
			}
			//set TotalPages
			List<String> choiceStrArray = XMLUtil.getChildren( priority );
			//set charts per page
			baseBean.setNumPerPage(CHOICE_COUNT_PER_PAGE);
			int totalCount=0;
			int totalChosen=0;
			int page = 1;
			
			PageBaseBean pageBean = null;//initial page bean
			for( String choiceStr: choiceStrArray ) {
				String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
				totalChosen+= Integer.valueOf(chosen);
			}
			for( String choiceStr: choiceStrArray ) {
				if(totalCount==0){
					pageBean = new PageBaseBean();
					pageBean.setPage(page++);
				}else if(totalCount%CHOICE_COUNT_PER_PAGE==0){
					//finish one page bean
					baseBean.addPageBaseBean(pageBean);
					//start next page bean
					pageBean = new PageBaseBean();
					pageBean.setPage(page++);
				}
				String index = XMLUtil.getPropertyValue( choiceStr , "Index");
				String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
				String text = XMLUtil.getPropertyValue( choiceStr , "Text");
				
				ChoiceResult cr = new ChoiceResult(Integer.valueOf(index),Integer.valueOf(chosen),text,text);
				cr.setShare((Double.valueOf(chosen))/totalChosen);
				pageBean.addChoiceResult(cr);
				
				totalCount++;
			}
			baseBean.setTotalPages(page-1);
			baseBean.setTotalCount(totalCount);
			baseBean.addPageBaseBean(pageBean);
//			baseBeanList.add(baseBean);
			sum++;
		}
		
		//no data found
		if(sum==0){
			return null;
		}
		
		return baseBean;
	}
	
	public static List<BaseBean> parseBaseBeanListForPriority(String content){
		List<BaseBean> baseBeanList = new ArrayList<BaseBean>();
		List<String> priorityList = XMLUtil.getChildren( content );

		//set questionType
		String type = XMLUtil.getPropertyValue( content , "Type");
		//set questionIndex
		String slideIndex = XMLUtil.getPropertyValue( content , "SlideIndex");
		//set totalQuestionCount
		String totalQuestionCount = XMLUtil.getPropertyValue( content , "TotalQuestionCount");
		if(totalQuestionCount==null){
			totalQuestionCount = "6";
		}
		//set questionName
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");

		//set answerTextStyle
		String answerTextStyle = XMLUtil.getPropertyValue( content , "AnswerTextStyle");
		
		for( String priority: priorityList ) {
//			String level = XMLUtil.getPropertyValue( priority , "level");
			BaseBean baseBean = new BaseBean();

			//set TotalPages
			List<String> choiceStrArray = XMLUtil.getChildren( priority );
			//set charts per page
			baseBean.setNumPerPage(CHOICE_COUNT_PER_PAGE);
			int totalCount=0;
			int totalChosen=0;
			int page = 1;
			
			PageBaseBean pageBean = null;//initial page bean
			for( String choiceStr: choiceStrArray ) {
				String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
				totalChosen+= Integer.valueOf(chosen);
			}
			for( String choiceStr: choiceStrArray ) {
				if(totalCount==0){
					pageBean = new PageBaseBean();
					pageBean.setPage(page++);
				}else if(totalCount%CHOICE_COUNT_PER_PAGE==0){
					//finish one page bean
					baseBean.addPageBaseBean(pageBean);
					//start next page bean
					pageBean = new PageBaseBean();
					pageBean.setPage(page++);
				}
				String index = XMLUtil.getPropertyValue( choiceStr , "Index");
				String chosen = XMLUtil.getPropertyValue( choiceStr , "Chosen");
				String text = XMLUtil.getPropertyValue( choiceStr , "Text");
				
				ChoiceResult cr = new ChoiceResult(Integer.valueOf(index),Integer.valueOf(chosen),text,text);
				cr.setShare((Double.valueOf(chosen))/totalChosen);
				pageBean.addChoiceResult(cr);
				
				totalCount++;
			}
			baseBean.setTotalPages(page-1);
			baseBean.setTotalCount(totalCount);
			baseBean.addPageBaseBean(pageBean);
			
			baseBean.setQuestionType(Integer.valueOf(type));
			baseBean.setQuestionIndex(Integer.valueOf(slideIndex));
			baseBean.setTotalQuestionCount(Integer.valueOf(totalQuestionCount));
			
			baseBean.setQuestionText(questionName);
			baseBean.setAnswerTextStyle(Integer.valueOf(answerTextStyle));
			
			baseBeanList.add(baseBean);
			
		}
		
		return baseBeanList;
	}
}
